package edu.ccut.saturn.workflow.startWorkFlowModule.code;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.common.WorkFlowTools;
import edu.ccut.saturn.workflow.model.ActivityInfo;

public class GetParticipantFromActivity implements IParticipantGeter {

	public List<SaturnData> getParticipant(SaturnData workItemInstance,
			ActivityInfo currentActivityInfo, IParameterContext parameterContext)
			throws Exception {
		String userSerialNo= null;
		String userName = null;
		List<SaturnData> wiParticipantDatas = new ArrayList<SaturnData>();
		// 注：不考虑启动者情况
		// 暂不实现上一活动（如果是上一活动，assistant签名并没有提交，会查不到值）
		Connection conn = parameterContext.getConnection();
		String userFrom = currentActivityInfo.getUserFrom();
		String activity_define_id = currentActivityInfo.getId();
		String process_instance_id = workItemInstance.get("process_instance_id")+"";
		
		ArrayList<HashMap<String, String>> userList=  getParticipantFromActivity(
				process_instance_id	,userFrom,conn);
		//TODO 4.21改。原因:代办的目标应该是工作项，这里应该查找代理，将代理的活动的参与者中加入代理人。
		/*//根据userList 循环核对是否有过代办在当前活动上，如果有，user要换成代办的人
		ArrayList<HashMap<String, String>> userDelegateList= new ArrayList<HashMap<String,String>>();
		for(HashMap<String,String> user:userList){
			userDelegateList.add(userCheckDelegate(user,process_instance_id,activity_define_id,conn));
		}*/
		//for(HashMap<String,String> user:userDelegateList){
		for(HashMap<String,String> user:userList){
			userSerialNo = user.get("userSerialNo");
			userName = user.get("userName");
			SaturnData wiParticipantData = WorkFlowTools
			.getParticipantInstanceWithValues( userSerialNo,
					userName);
	        wiParticipantDatas.add(wiParticipantData);
		}
		
		return wiParticipantDatas;
	}
/**
 * 校验参与者是否有代办在当前活动，有则替换为代办人
 * @param user
 * @return
 * @throws Exception 
 */
/*	private HashMap<String, String> userCheckDelegate(
			HashMap<String, String> user,String process_instance_id,String activity_define_id,Connection conn) throws Exception {
		String userid_old = user.get("userSerialNo");
		String userid_new = null;//存储真正的参与者
		String sql = "select * from wf_delegate where process_instance_id ='"+process_instance_id+"' and activity_define_id='"+activity_define_id+"' and delegate_from='"+userid_old+"'";
		ArrayList<SaturnData> delegateResultList = Operation.dataBase().queryBySQL(Constants.SATURNDATA_WF_DELEGATE,sql, conn);
		HashMap<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("process_instance_id", process_instance_id);
		paramMap.put("activity_define_id", activity_define_id);
		paramMap.put("delegate_from", userid_old);
		ArrayList<SaturnData> delegateResultList = Operation.dataBase().queryBySaturnSql(Constants.SATURNDATA_WF_DELEGATE,
				"QUERY_DELEGATE_IN_ACTIVITY", paramMap, conn);
		if(delegateResultList==null){//没有代办
			return user;
		}else{//有代办
			userid_new = delegateResultList.get(0).get("delegate_to")+"";
			HashMap<String,String> user_new= new HashMap<String,String>();
			user_new.put("userSerialNo", userid_new);
			user_new.put("userName", WorkFlowTools.getUserNameById(userid_new, conn));
			return userCheckDelegate(user_new,process_instance_id,activity_define_id,conn);
		}
	}*/
	/**
     * 从指定的活动获取参与者
     * @param process_instance_id
     * @param activity_define_id
     * @param conn
     * @return
     * @throws Exception
     */
	public static ArrayList<HashMap<String, String>> getParticipantFromActivity(
			String process_instance_id, String activity_define_id,
			Connection conn) throws Exception {
		ArrayList<HashMap<String, String>> userList = new ArrayList<HashMap<String,String>>();
		/*String sql = "select assistant,assistant_name from wf_workitem_instance"
				+ " where activity_define_id = '"
				+ activity_define_id
				+ "'"
				+ " and process_instance_id = '" + process_instance_id + "'";
		ArrayList<SaturnData> workitemList = Operation.dataBase().queryBySQL(
				Constants.SATURNDATA_WF_WORKITEM_INSTANCE, sql, conn);*/
		HashMap<String,Object> paramMap = new HashMap<String, Object>();
		paramMap.put("activity_define_id",activity_define_id);
		paramMap.put("process_instance_id", process_instance_id);	
		ArrayList<SaturnData> workitemList = Operation.dataBase().queryBySaturnSql(Constants.SATURNDATA_WF_WORKITEM_INSTANCE,
				"GET_PARTICIPANT_FROM_ACTIVITY", paramMap, conn);
		String userSerialNo = null;
		String userName = null;
		for (SaturnData workitem : workitemList) {
			// TODO 这里返回的可能是多个结果（当多工作项时）， 
			HashMap<String,String> user= new HashMap<String,String>();
			userSerialNo = (String) workitem.get("assistant");
			userName = (String) workitem.get("assistant_name");
			user.put("userSerialNo", userSerialNo);
			user.put("userName", userName);
			userList.add(user);
		}
		return userList;
	}
}
